//
//  KRXKAlartView.m
//  KEREN
//
//  Created by MW on 2023/5/5.
//

#import "KRXKAlartView.h"
#import "KRFeedbackViewController.h"
#import "KROnlienBurglarizedVC.h"
@implementation KRXKAlartView
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        self.backgroundColor = [UIColor whiteColor];
        BubbleLayer *bbLayer = [[BubbleLayer alloc]initWithSize:self.bounds.size];
        // 矩形框的圆角半径
        bbLayer.cornerRadius = 25;
        // 凸起那部分暂且称之为“箭头”，下面的参数设置它的形状
        bbLayer.arrowDirection = ArrowDirectionLeft;
        bbLayer.arrowHeight = 10;   // 箭头的高度（长度）
        bbLayer.arrowWidth = 14;    // 箭头的宽度
        bbLayer.arrowDirection = ArrowDirectionBottom;
        bbLayer.arrowPosition = 1;// 箭头的相对位置
        bbLayer.arrowRadius = 0;    // 箭头处的圆角半径
        [self.layer setMask:[bbLayer layer]];
        [self addUI];
    }
    return self;
}

- (void)addUI{
    UILabel *titleLbl = [UILabel setLblText:@"小可服务" font:BFont(20) withColor:Black_Color inSubView:self];
    [titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(16);
        make.left.offset(24);
    }];
    
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:closeBtn];
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-6);
        make.width.height.offset(40);
        make.centerY.equalTo(titleLbl);
    }];
    [closeBtn setImage:[UIImage imageNamed:@"NavBar_Default_Close"] forState:UIControlStateNormal];
    [closeBtn addTarget:self action:@selector(closeBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *layerView = [[UIView alloc] init];
    [self addSubview:layerView];
    [layerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(24);
        make.top.offset(56);
        make.bottom.offset(-30);
        make.right.offset(-24);
    }];
    layerView.backgroundColor = Color_F2F6FC;
    NSArray *imgsArr = @[@"KJ_FK_icon",@"KJ_PHONE_icon",@"KJ_BX_icon"];
    //NSArray *titleArr = @[@"意见反馈",@"拨打400",@"在线报修"];
    NSArray *titleArr = @[@"意见反馈"];

    for (int i = 0; i < 1; i ++) {
        UIView *buttonView = [[UIView alloc] init];
        buttonView.backgroundColor = [UIColor clearColor];
        [layerView addSubview:buttonView];
        [buttonView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(1 * (DEVICE_WIDTH - 96) / 3);
            make.top.bottom.offset(0);
            make.width.offset((DEVICE_WIDTH - 96) / 3);
        }];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imgsArr[i]]];
        [buttonView addSubview:imageView];
        float imageWidth = (DEVICE_WIDTH - 96 - 64) / 3;
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(20);
            make.width.height.offset(imageWidth);
            make.centerX.equalTo(buttonView);
        }];
        UILabel *titleLbl = [UILabel setLblText:titleArr[i] font:Font(14) withColor:Black_Color inSubView:buttonView];
        [titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.offset(-20);
            make.centerX.equalTo(buttonView);
        }];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(buttonViewClick:)];
        [buttonView addGestureRecognizer:tap];
        buttonView.tag = 500 + i;
    }
}

- (void)buttonViewClick:(UITapGestureRecognizer *)tap{
    if(tap.view.tag == 500){
        UserInfo *user = USERINFO;
        if(user.token.length < 1){
            [[UserInfoManager sharedInstance] loginOut];
            return;
        }
        KRFeedbackViewController *vc = [[KRFeedbackViewController alloc] init];
        [[ClassManager shareInstance].currentViewController.navigationController pushViewController:vc animated:YES];
    }else if (tap.view.tag == 501){
        NSMutableString *str= [[NSMutableString alloc] initWithFormat:@"telprompt://%@",@"400-6634-576"];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str] options:@{} completionHandler:^(BOOL success) {
        }];
    }else{
        UserInfo *user = USERINFO;
        if(user.token.length < 1){
            [[UserInfoManager sharedInstance] loginOut];
            return;
        }
        KROnlienBurglarizedVC *vc = [[KROnlienBurglarizedVC alloc] init];
        [[ClassManager shareInstance].currentViewController.navigationController pushViewController:vc animated:YES];
    }
    [self closeBtnClick];
}

- (void)closeBtnClick{
    [self removeFromSuperview];
}

@end
